/*
 * Main.java
 *
 * Created on August 18, 2006, 7:46 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package xorgconfusbinputdevicecorrector;

import java.io.*;

/**
 *
 * @author robert
 */
public class Main {

    /** Creates a new instance of Main */
    public Main() {
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            // parameters to cat
            String cmd = "cat /proc/bus/input/devices";
            // where to store each line of InputStream
            String line = null;
            // execute cat
            Process p = Runtime.getRuntime().exec(cmd);
            // store the output
            // create a StringBuffer for the file's contents
            StringBuffer deviceDetails = new StringBuffer();
            BufferedReader input =
                new BufferedReader(
                    new InputStreamReader(p.getInputStream()
                )
            );
            while ((line = input.readLine()) != null) {
                deviceDetails.append(line);
            }
            // close the InputStream
            input.close();
            // array indexes
            // value
            int i = 0;
            // seats
            int seat0 = i++;
            int seat1 = i++;
            // reset value
            i = 0;
            // devices
            int device0 = i++;
            int device1 = i++;
            // reset value
            i=0;
            // attributes
            int Identifier = i++;
            int Name = i++;
            int Handler = i++;
            int Value1 = i++;
            int Value2 = i++;
            // our USB Devices
            String[][][] USBDevices = new String[2][2][i];
            // Trackball for seat0
            USBDevices[seat0][device0][Name] = "Logitech Trackball";
            USBDevices[seat0][device0][Identifier] = "Mouse0";
            USBDevices[seat0][device0][Handler] = "mouse";
            // Keyboard for seat0
            USBDevices[seat0][device1][Name] = "MONTEREY USB K/B WITH ACPI";
            USBDevices[seat0][device1][Identifier] = "Keyboard0";
            USBDevices[seat0][device1][Handler] = "event";
            // Trackball for seat1
            USBDevices[seat1][device0][Name] = "Logitech USB Trackball";
            USBDevices[seat1][device0][Identifier] = "Mouse1";
            USBDevices[seat1][device0][Handler] = "mouse";
            // seat1 uses a PS/2 keyboard so it only has one device
            USBDevices[seat1][device1][Name] = null;
            USBDevices[seat1][device1][Identifier] = null;
            
            // for each seat
            for(int seats = 0; seats < USBDevices.length; seats++) {
                // for each device in that seat
                for(int devices = 0; devices < USBDevices[seats].length; devices++) {
                    // if the devices is specified
                    if(USBDevices[seats][devices][Name] != null) {
                        // get the value of the handler
                        USBDevices[seats][devices][Value1] =
                            // substring
                            deviceDetails.toString().substring(
                                // from indexOf Handler from indexOf Name
                                deviceDetails.toString().indexOf(
                                    USBDevices[seats][devices][Handler],
                                    deviceDetails.toString().indexOf(USBDevices[seats][devices][Name])
                                ),
                                // to indexOf space from indexOf Handler from indexOf Name
                                deviceDetails.toString().indexOf(
                                    " ",
                                    deviceDetails.toString().indexOf(
                                        USBDevices[seats][devices][Handler],
                                        deviceDetails.toString().indexOf(USBDevices[seats][devices][Name])
                                    )
                                )
                            );
                        // if there are more than one entry with the same name, enter a value for Value2
                        if(
                            // indexOf Name from indexOf Handler from indexOf Name
                            deviceDetails.toString().indexOf(
                                // string
                                USBDevices[seats][devices][Name],
                                // from
                                deviceDetails.toString().indexOf(
                                    // string
                                    USBDevices[seats][devices][Handler],
                                    // from
                                    deviceDetails.toString().indexOf(
                                        // string from 0
                                        USBDevices[seats][devices][Name]
                                    )
                                )
                            ) != -1
                        ) {
                            // set Value2
                            USBDevices[seats][devices][Value2] =
                                // substring
                                deviceDetails.toString().substring(
                                    // from indexOf Handler from indexOf Name from indexOf Handler from indexOf Name
                                    deviceDetails.toString().indexOf(
                                        // string
                                        USBDevices[seats][devices][Handler],
                                        // from
                                        deviceDetails.toString().indexOf(
                                            // string
                                            USBDevices[seats][devices][Name],
                                            // from
                                            deviceDetails.toString().indexOf(
                                                // string
                                                USBDevices[seats][devices][Handler],
                                                // from
                                                deviceDetails.toString().indexOf(
                                                    // string from 0
                                                    USBDevices[seats][devices][Name]
                                                )
                                            )
                                        )
                                    ),
                                    // to indexOf space from indexOf Handler from indexOf Name from indexOf Handler from indexOf Name
                                    deviceDetails.toString().indexOf(
                                        // string
                                        " ",
                                        // from
                                        deviceDetails.toString().indexOf(
                                            // string
                                            USBDevices[seats][devices][Handler],
                                            // from
                                            deviceDetails.toString().indexOf(
                                                // string
                                                USBDevices[seats][devices][Name],
                                                // from
                                                deviceDetails.toString().indexOf(
                                                    // string
                                                    USBDevices[seats][devices][Handler],
                                                    // from
                                                    deviceDetails.toString().indexOf(
                                                        // string from 0
                                                        USBDevices[seats][devices][Name]
                                                    )
                                                )
                                            )
                                        )
                                    )
                                );
                        }
                    }
                }
            }
            // output the values found
            System.out.println(USBDevices[seat0][device0][Value1]);
            System.out.println(USBDevices[seat0][device1][Value1]);
            System.out.println(USBDevices[seat0][device1][Value2]);
            System.out.println(USBDevices[seat1][device0][Value1]);
            // open xorg.conf
            File xorgConf = new File("/etc/X11/xorg.conf");
            // read the file
            FileInputStream fis = new FileInputStream(xorgConf);
            // write a new version of the file to a StringBuffer
            StringBuffer xorgConfContents = new StringBuffer();
            // use a BufferedReader to read one line at a time
            input =
                new BufferedReader(
                    new InputStreamReader(fis)
                );
            // indexes
            int seat = -1;
            int device = -1;
            int value = -1;
            // while there are more lines
            while ((line = input.readLine()) != null) {
                // if the line identifies one of our usb devices
                if(
                    line.toLowerCase().indexOf("identifier") != -1
                &&
                    (
                        line.indexOf('#') > line.toLowerCase().indexOf("identifier")
                    ||
                        line.indexOf('#') == -1
                    )
                ) {
                    // set the indexes
                    if(
                        line.toLowerCase().indexOf(
                            USBDevices[seat0][device0][Identifier].toLowerCase()
                        ) != -1
                    ) {
                        seat = seat0;
                        device = device0;
                        value = Value1;
                    } else if(
                        line.toLowerCase().indexOf(
                            USBDevices[seat0][device1][Identifier].toLowerCase() + "b"
                        ) != -1
                    ) {
                        seat = seat0;
                        device = device1;
                        value = Value2;
                    } else if(
                        line.toLowerCase().indexOf(
                            USBDevices[seat0][device1][Identifier].toLowerCase()
                        ) != -1
                    ) {
                        seat = seat0;
                        device = device1;
                        value = Value1;
                    } else if(
                        USBDevices[seat1][device0][Identifier] != null
                    &&
                        line.toLowerCase().indexOf(
                            USBDevices[seat1][device0][Identifier].toLowerCase()
                        ) != -1
                    ) {
                        seat = seat1;
                        device = device0;
                        value = Value1;
                    }
                // if the line is one of our usb device lines
                } else if(
                    seat != -1
                &&
                    line.toLowerCase().indexOf("option") != -1
                &&
                    line.toLowerCase().indexOf("device") > line.toLowerCase().indexOf("option")
                &&
                    line.toLowerCase().indexOf("/dev/input") > line.toLowerCase().indexOf("device")
                &&
                    (
                        line.indexOf('#') > line.toLowerCase().indexOf("/dev/input")
                    ||
                        line.indexOf('#') == -1
                    )
                ) {
                    // replace line
                    line = "\tOption \"Device\" \"/dev/input/" + USBDevices[seat][device][value] + "\" # replaced";
                    seat = -1;
                }
                xorgConfContents.append(line + "\n");
            }
            // close the InputStream
            input.close();
            System.out.println(xorgConfContents);
            FileOutputStream fos = new FileOutputStream("/etc/X11/xorg.conf");
            fos.write(xorgConfContents.toString().getBytes());
            fos.flush();
            fos.close();
        } catch(java.io.IOException e) {
            e.printStackTrace();
        }
    }
}